package com.beyondsoft.common.activiti.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Base64Utils {
    private static final Logger log = LoggerFactory.getLogger(Base64Utils.class);
    public static final String DEFAULT_CHARSET = "UTF-8";

    public Base64Utils() {
    }

    public static byte[] encode(String src) {
        return encode(src, "UTF-8");
    }

    public static String encodeStr(String src) {
        try {
            return new String(encode(src, "UTF-8"), "UTF-8");
        } catch (Exception var2) {
            log.error(var2.getMessage(), var2);
            throw new RuntimeException("base64编码异常");
        }
    }

    public static byte[] encode(String src, String charset) {
        Object var2 = null;

        try {
            if (StringUtils.isBlank(charset)) {
                charset = "UTF-8";
            }

            byte[] b = src.getBytes(charset);
            byte[] res = encode(b);
            return res;
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            throw new RuntimeException("base64加密异常");
        }
    }

    public static byte[] encode(byte[] src) {
        return Base64.encodeBase64(src);
    }

    public static byte[] decode(String src) {
        return decode(src, "UTF-8");
    }

    public static String decodeStr(String src) {
        try {
            return new String(decode(src, "UTF-8"), "UTF-8");
        } catch (Exception var2) {
            log.error(var2.getMessage(), var2);
            throw new RuntimeException("base64解码异常");
        }
    }

    public static byte[] decode(String src, String charset) {
        Object var2 = null;

        try {
            if (StringUtils.isBlank(charset)) {
                charset = "UTF-8";
            }

            byte[] b = src.getBytes(charset);
            byte[] res = decode(b);
            return res;
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            throw new RuntimeException("base64解密异常");
        }
    }

    public static byte[] decode(byte[] src) {
        return Base64.decodeBase64(src);
    }
}

